package com.kede.api.stand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kede.api.stand.domain.SysArea;
import com.kede.api.stand.mapper.SysAreaMapper;
import com.kede.api.stand.pub.AreaInfoVo;
import com.kede.api.stand.service.SysAreaService;
import com.kede.dictenum.AreaDictEnum;
import org.springframework.stereotype.Service;

/**
* @author 11856
* @description 针对表【sys_area(与高德地图一致的区县划分)】的数据库操作Service实现
* @createDate 2023-04-26 14:47:00
*/
@Service
public class SysAreaServiceImpl extends ServiceImpl<SysAreaMapper, SysArea>
    implements SysAreaService{

    @Override
    public AreaInfoVo findDetailByAdcode(Integer adcode) {
        LambdaQueryWrapper<SysArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysArea::getAdcode,adcode);
        SysArea sysArea = getOne(queryWrapper);
        if (sysArea == null){
            throw new NullPointerException("无效的地区编码:"+adcode);
        }
        AreaDictEnum areaDictEnum = AreaDictEnum.valueOfCode(sysArea.getLevel());
        AreaInfoVo areaInfoVo = new AreaInfoVo();
        SysArea province;
        switch (areaDictEnum) {
            case province:
                //省
                areaInfoVo.setProvinceCode(sysArea.getAdcode());
                areaInfoVo.setProvinceName(sysArea.getName());
                break;
            case ciry:
                //市
                province = getById(sysArea.getPid());
                areaInfoVo.setProvinceCode(province.getAdcode());
                areaInfoVo.setProvinceName(province.getName());
                areaInfoVo.setCityCode(sysArea.getAdcode());
                areaInfoVo.setCityName(sysArea.getName());

                break;
            case area:
                //区
                SysArea city = getById(sysArea.getPid());
                province = getById(city.getPid());
                areaInfoVo.setProvinceCode(province.getAdcode());
                areaInfoVo.setProvinceName(province.getName());
                areaInfoVo.setCityCode(city.getAdcode());
                areaInfoVo.setCityName(city.getName());
                areaInfoVo.setAreaCode(sysArea.getAdcode());
                areaInfoVo.setAreaName(sysArea.getName());
                break;
        }
//        DictService
        return areaInfoVo.toAddress();
    }

}




